﻿@model List<Yes.Domain.Entity.CategoryEntity>
@{
    ViewBag.Title = "Index";
    Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}

<div class="main-wrap">

    <div class="crumb-wrap">
        <div class="crumb-list"><i class="icon-font"></i><a href="@(Url.Action("Index","Home"))">首页</a><span class="crumb-step">&gt;</span><span class="crumb-name">分类管理</span></div>
    </div>
    <div class="result-wrap">
        <form name="myform" id="myform" method="post">
            <div class="result-title">
                <div class="result-list">
                    <a href="@(Url.Action("Add","Category"))"><i class="icon-font"></i>新增分类</a>
                    <a id="batchDel" href="javascript:void(0)"><i class="icon-font"></i>批量删除</a>
                    <a id="updateOrd" href="javascript:void(0)"><i class="icon-font"></i>更新排序</a>
                </div>
            </div>
            <div class="result-content">
                <table class="result-tab" width="100%">
                    <tr>
                        <th class="tc" width="5%"><input class="allChoose" name="checkAll" id="checkAll" type="checkbox"></th>
                        <th>排序</th>
                        <th>ID</th>
                        <th>名称</th>
                        <th>文章数</th>
                    </tr>
                    @foreach (var category in Model)
                    {
                        <tr>
                            <td class="tc"><input name="checkname" value="@(category.Id)" type="checkbox"></td>
                            <td>
                                <input name="Id" value="@(category.Id)" type="hidden">
                                <input class="common-input sort-input" name="SortId" value="@(category.SortId)" type="text">
                            </td>
                            <td>@(category.Id)</td>
                            <td title="@(category.Name)">
                                <a href="@(Url.Action("Add","Category",new {id = category.Id}))" title="@(category.Name)">@(category.Name)</a>
                            </td>
                            <td>@(category.ArticleCount)</td>
                        </tr>
                    }
                    @if (Model.Count == 0)
                    {
                        <tr>
                            <td class="tc" colspan="99"><a href="@(Url.Action("Add","Category"))">还没有任何分类，去添加</a></td>
                        </tr>
                    }

                </table>

            </div>
        </form>
    </div>
</div>

<script>
    $(function () {
        $("#checkAll").click(
          function () {
              if (this.checked) {
                  $("input[name='checkname']").prop('checked', true);
              } else {
                  $("input[name='checkname']").prop('checked', false);
              }
          }
        );

        $("#batchDel").click(
          function () {
              var ids = $("input[name='checkname']:checked").map(function () { return this.value; }).get();

              $.ajax({
                  type: "POST",
                  url: "@(Url.Action("Delete", "Category"))",
                  data: { ids: ids },
                  traditional:true,//数组参数不带[]
                  success: function (result) {
                      if (result.errorMessage) {
                          var error = result.errorMessage;
                          if (result.data) {
                              error += "<br>" + result.data;
                          }
                          layer.alert(error, { icon: 5 }, function() {
                              location.reload();
                          });
                      } else {
                          location.reload();
                      }
                  }
              });
          }
        );

        $("#updateOrd").click(
          function () {
              var ids = $("input[name='checkname']:checked").map(function () { return this.value; }).get();
              var sortids = $("input[name='checkname']:checked").parent().next("td").find("input[name='SortId']").map(function () { return this.value; }).get();

              
              $.ajax({
                  type: "POST",
                  url: "@(Url.Action("UpdateOrder", "Category"))",
                  data: { ids: ids, sortids: sortids },
                  traditional:true,//数组参数不带[]
                  success: function (result) {
                      if (result.errorMessage) {
                          var error = result.errorMessage;
                          if (result.data) {
                              error += "<br>" + result.data;
                          }
                          layer.alert(error, { icon: 5 }, function() {
                              location.reload();
                          });
                      } else {
                          location.reload();
                      }
                  }
              });
          }
        );
    });

</script>